package LinkList;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: LinkList
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-19  07:13
 * @Description: TODO
 * @Version: 1.0
 */
public class 链表反转206 {

    class ListNode {
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    // 反转链表
    public ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode future = curr.next;
            curr.next = pre;
            pre = curr;
            curr = future;
        }
        return pre;
    }

    // 采用两个指针的方式来实现 必须考虑传入的值是否为空
    public ListNode reverseList2(ListNode head) {
        if (head == null) {
            return head;
        }
        //设置新的节点
        ListNode dumy = new ListNode(-1);
        dumy.next = head;
        // 设置新的两个标志位
        ListNode pre = dumy;
        ListNode curr = head;
        while (curr.next != null) {
            ListNode future = curr.next;
            curr.next = future.next;
            future.next = dumy.next;
            pre.next = future;
        }
        return dumy.next;
    }
}
